#include <stdio.h>
#include <stdlib.h>
#include "zerdlib.h"
#include "UART.h"

_FBS(BWRP_WRPROTECT_OFF & RBS_NO_BOOT_RAM & BSS_NO_BOOT_CODE)
_FSS(SWRP_WRPROTECT_OFF & RSS_NO_SEC_RAM & SSS_NO_SEC_CODE)
_FGS(GWRP_OFF & GSS_OFF)
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF)
_FOSC(FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_ON & POSCMD_NONE)
_FWDT(FWDTEN_OFF)
_FPOR(PWMPIN_ON & HPOL_ON & LPOL_ON & FPWRT_PWR1 & ALTI2C_OFF)
_FICD(JTAGEN_OFF & ICS_PGD1)


int pos[6] = {0b000110, 0b100100, 0b100001, 0b001001, 0b011000, 0b010010};
int main(void) {
    int j=0;
    oscConfig();
    SetRAAsOutput();
    SetRBAsOutput();

    /*
    P1TCONbits.PTEN = 1;

    // up/down mode
    P1TCONbits.PTMOD = 0b10;

    // 100kHz
    P1TCONbits.PTCKPS = 00;
    PTPER = 200;

    // special event
    P1SECMPbits.SEVTDIR = 0;
    P1SECMPbits.SEVTCMP = 200;
    PWM1CON2bits.SEVOPS = 0;

    // choix des pattes : independant et tout en PWM
    PWM1CON1 = 0b0000000011111111;

    // Update du DC en fin de période
    PWM1CON2bits.IUE = 0;
    PWM1CON2bits.UDIS = 0;

    // mise à 0 des PWM
    P1DC1 = P1DC2 = P1DC3 = 0;

    P1DC2 = 160;
    P1DC3 = 320;
    */


    while(1) {
        long i;
        for(i=0;i<500000;i++);
        SetRAOutput(j);
        j = (j+1)%16;
        InvRAOutput();
        SetRBOutput((pos[j%6])<<10);
        /*P1DC1 = (P1DC1+80)%400;
        P1DC2 = (P1DC2+80)%400;
        P1DC3 = (P1DC3+80)%400;*/
    }
}


